<template>
  <div class="xtx-city" ref="target" >
    <div class="select" @click="toggleDialog" :class="{active}" >
      <span class="placeholder">{{fullLocation}}</span>
      <span class="value" ></span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span class="ellipsis" v-for="item in cityData" :key="item.code" @click="changeItem(item)">{{item.name}}</span>
    </div>
  </div>
</template>
<script>
import { ref, onMounted, reactive } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { getCityData } from '@/hooks'
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default:''
    }
  },
  setup (props, {emit}) {
    const cityData = ref([])
     const changeResult = reactive({
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      fullLocation: ''  
    })
    const getData = async() => {
      const res = await getCityData()
      cityData.value = res
    }
    onMounted(() => {
      getData()
    })
    // 控制展开收起,默认收起
    const active = ref(false)
    const openDialog = () => {
      active.value = true
    }
    const closeDialog = () => {
      active.value = false
      getData()
    }
    // 切换展开收起
    const toggleDialog = () => {
      if (active.value) closeDialog()
      else openDialog()
    }
    // 点击其他位置隐藏
    const target = ref(null)
    onClickOutside(target, () => {
      closeDialog()
    })
    // 找子级省份
    const changeItem = (item) => {
      // 省份
      if (item.level === 0) {
        changeResult.provinceCode = item.code
        changeResult.provinceName = item.name
      }
      // 市
      if (item.level === 1) {
        changeResult.cityCode = item.code
        changeResult.cityName = item.name
      }
      if(item.areaList){
        cityData.value = item.areaList
      }else{
        // 地区
        changeResult.countyCode = item.code
        changeResult.countyName = item.name
        // 展示数据
        changeResult.fullLocation = changeResult.provinceName + '-' + changeResult.cityName + '-' + changeResult.countyName 
        emit('change', changeResult)
        closeDialog()
      }
    }
    return { active, toggleDialog, target, cityData, changeItem }
  }
}
</script>
<style scoped lang="scss">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;  
  .select {
    border: 1px solid #e4e4e4;
    height: 40px;
    border-radius: 2px;
    padding: 0 10px;
    line-height: 38px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>